package sipus.action.maintenance;

import java.net.InetAddress;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.sys.ExecutionsCtrl;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zkplus.hibernate.HibernateUtil;
import org.zkoss.zul.A;
import org.zkoss.zul.Html;
import org.zkoss.zul.Image;
import org.zkoss.zul.Include;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.Window;

import sipus.action.master.helper.DaftarPenggunaOnline;
import sipus.common.Common;
import sipus.database.model.AccessedUsers;
import sipus.database.model.OnlineUsers;
import sipus.delivery.email.sender.MailHelper;


public class LoginAction extends GenericAutowireComposer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 187958355469911830L;
	private Include iframe;
	private Window addWindow;

	private Image header;
	private A footer;
	private Html pemberitahuan;

	private Toolbarbutton usersOnline;
	private Vbox menubar;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		// TODO Auto-generated method stub
		super.doAfterCompose(comp);
		Common.setUserAccess((HttpServletRequest) execution.getNativeRequest());
		if (session.getAttribute("usersTemp") != null) {
			execution.sendRedirect("/pages/main/main.zul");
			return;
		}

		session.setAttribute(Attributes.PREFERRED_LOCALE,
				new Locale("in", "ID"));
		session.removeAttribute("usersTemp");

		Common.encripSemua();
		Common.setting(header, footer, menubar, pemberitahuan);

		// settingHeader();

		onUpdateUserOnline(null);
	}

	public void onLihatOnline(Event event) throws Exception {
		DaftarPenggunaOnline window = new DaftarPenggunaOnline(
				(HttpServletRequest) ExecutionsCtrl.getCurrent()
						.getNativeRequest());
		ExecutionsCtrl.getCurrentCtrl().getCurrentPage().getFirstRoot()
				.appendChild(window);
		window.setVisible(true);
		window.onModal();
	}

	public void onClickPMB() throws Exception {
		iframe.setSrc("/pages/master/biodata_calon_mahasiswa.zul");
	}

	public void onClickHome() throws Exception {
		iframe.setSrc("/pages/main/blank.zul");
	}

	public void onClickMahasiswa() throws Exception {
		iframe.setSrc("/pages/master/kegiatan_per_mahasiswa.zul");
	}

	public void onClickPMDK() throws Exception {
		iframe.setSrc("/pages/master/pendaftaranPMDK.zul");
	}

	public void onUpdateUserOnline(Event e) {
		try {
			HttpServletRequest request = (HttpServletRequest) ExecutionsCtrl
					.getCurrent().getNativeRequest();
			String req = request.getRequestURL().toString();
			req = req.replaceAll("http://", "");
			req = req.split(":")[0];
			req = req.split("/")[0];
			Session session = HibernateUtil.currentSession();
			Number count = (Number) session.createCriteria(AccessedUsers.class)
					.add(Restrictions.eq("host", req))
					.setProjection(Projections.count("id")).uniqueResult();

			Number countOnline = (Number) session
					.createCriteria(OnlineUsers.class)
					.add(Restrictions.eq("host", req))
					.setProjection(Projections.count("id")).uniqueResult();

			InetAddress addr = InetAddress.getLocalHost();
			String hostname = addr.getHostName();
			String result = "User(s) access: " + count + ", logged in: "
					+ countOnline + " (" + hostname + ")";
			// System.out.println("result = " + result);
			usersOnline.setLabel(result);

		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void onForgotPassword() {
		MailHelper mailHelper = new MailHelper();
		mailHelper.tampil(addWindow);

	}

}
